package model;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.Date;
import java.util.Iterator;
import java.util.List;

public class Device extends ModelBase
{
	private String name;
	private Date buildDate;
	private String display;
	private String operatingSystem;
	private int screenSize;
	private List<App> apps;
	private Manufacturer manufacturer;
	

	public Device()
	{
	}
	
	public Device(String name)
	{
		setName(name);
		apps = new ArrayList<App>();
	}
	
	public Device(String name, Date buildDate)
	{
		this(name);
		setBuildDate(buildDate);
	}
	
	public Device(String name, Date buildDate, String operatingSystem)
	{
		this(name, buildDate);
		setOperatingSystem(operatingSystem);
	}
	
	public Device(String name, Date buildDate, String operatingSystem, String display, int screenSize, Manufacturer manufacturer)
	{
		this(name, buildDate, operatingSystem);
		setDisplay(display);
		setScreenSize(screenSize);
		setManufacturer(manufacturer);
	}

	public void setName(String name) 
	{
		if (name == null || name.trim().isEmpty()) throw new IllegalArgumentException("Name must not be null");
		this.name = name;
	}
	
	public String getName() 
	{
		return name;
	}
	
	public void setBuildDate(Date buildDate) 
	{
		if(buildDate == null) throw new IllegalArgumentException("buildDate must not be null");
		this.buildDate = (Date) buildDate.clone();
	}
	
	public Date getBuildDate() 
	{
		return (Date) buildDate.clone();
	}
	
	public void setOperatingSystem(String operatingSystem) 
	{
		if (operatingSystem == null || operatingSystem.trim().isEmpty()) throw new IllegalArgumentException("Operating System must not be null");
		this.operatingSystem = operatingSystem;
	}
	
	public String getOperatingSystem() 
	{
		return operatingSystem;
	}
	
	public void setDisplay(String display) 
	{
		if (display == null || display.trim().isEmpty()) throw new IllegalArgumentException("Display must not be null");
		this.display = display;
	}
	
	public String getDisplay() 
	{
		return display;
	}
	
	public void setScreenSize(int screenSize) 
	{
		if(screenSize < 0 || screenSize > 50) throw new IllegalArgumentException("Screensize must be greater than 0 and less or equal 50");
		
		this.screenSize = screenSize;
	}
	
	public int getScreenSize() 
	{
		return screenSize;
	}
	
	public void setManufacturer(Manufacturer m)
	{
		if(m == null) throw new IllegalArgumentException("Manufacturer must not be null");
		this.manufacturer = m;
	}
	
	public Manufacturer getManufacturer()
	{
		return manufacturer;
	}
	
	public List<App> getList()
	{
		return apps;
	}
	
	public void addApp(App a)
	{
		if(a == null) throw new IllegalArgumentException("App must not be null");
		apps.add(a);
	}
	
	public void deleteAppsByName(String name)
	{
		if(name == null || name.trim().isEmpty()) throw new IllegalArgumentException("Name must not be null");
		if(apps.isEmpty()) throw new IllegalArgumentException("list is empty");
		
		Iterator<App> it = apps.iterator();
		while(it.hasNext())
		{
			App a = (App) it.next();
			if(a.getName().equals(name)) it.remove();
		}
	}
	
	public void deleteAppByCategory(String category)
	{
		if (category == null || category.trim().length() == 0) throw new IllegalArgumentException("category must not be null");
		if (apps.isEmpty()) throw new IllegalArgumentException("List is empty");

		Iterator<App> it = apps.iterator();
		while (it.hasNext())
		{
			App a = (App) it.next();
			if (a.getCategory().equals(category)) it.remove();
		}

	}
	
	public List<App> sortListByName()
	{
		List<App> sortedListbyName = new ArrayList<App>();
		sortedListbyName.addAll(apps);
		Collections.sort(sortedListbyName, new App.AppByNameComparator());
		return Collections.unmodifiableList(sortedListbyName);
	}
	
	public List<App> sortListByPrice()
	{
		List<App> sortedListbyPrice = new ArrayList<App>();
		sortedListbyPrice.addAll(apps);
		Collections.sort(sortedListbyPrice, new App.AppByPriceComparator());
		return Collections.unmodifiableList(sortedListbyPrice);
	}

	public List<App> sortListByCategory()
	{
		List<App> sortedListbyCategory = new ArrayList<App>();
		sortedListbyCategory.addAll(apps);
		Collections.sort(sortedListbyCategory, new App.AppByCategoryComparator());
		return Collections.unmodifiableList(sortedListbyCategory);
	}
	
	public Collection<App> getListByCategory(String category)
	{
		if (apps.isEmpty()) throw new IllegalStateException("List is empty");

		List<App> categoryList = new ArrayList<App>();

		for (App a : apps)
		{
			if (a.getCategory().equalsIgnoreCase(category)) categoryList.add(a);
		}

		if (categoryList.isEmpty()) return Collections.emptyList();
		return Collections.unmodifiableCollection(categoryList);
	}
	
	
	public Collection<App> getListByPrice(int price)
	{
		if (apps.isEmpty()) throw new IllegalStateException("List is empty");

		List<App> PriceList = new ArrayList<App>();

		for (App a : apps)
		{
			if (a.getPrice() == price) PriceList.add(a);
		}

		if (PriceList.isEmpty()) return Collections.emptyList();
		return Collections.unmodifiableCollection(PriceList);
	}
}

